//使用类型谓词
type Fish1 = {
  name: string,
  swim: () => void
}
type Bird1 = {
  name: string,
  fly: () => void
}

function isFish(pet: Fish1 | Bird1): pet is Fish1 {
  return (pet as Fish1).swim !== undefined
}

function getSmallPet(): Fish1 | Bird1 {
  let fish: Fish1 = {
    name: 'sharkey',
    swim: () => { }

  }

  let bird: Bird1 = {
    name: 'sparrow',
    fly: () => { }
  }

  return true ? bird : fish
}
let pet = getSmallPet()
if (isFish(pet)) {
  pet.swim()
} else {
  pet.fly()
}

const zoo: (Fish1 | Bird1)[] = [getSmallPet(), getSmallPet(), getSmallPet()]
const underWater1: Fish1[] = zoo.filter(isFish)
const underWater2: Fish1[] = zoo.filter(isFish) as Fish1[]

const underWater3: Fish1[] = zoo.filter((pet): pet is Fish1 => {
  if (pet.name === 'frog') {
    return false
  }
  return isFish(pet)
})
